package com.educhains.web.ctrl.frontend;


import com.educhains.module.blog.service.BlogService;
import com.educhains.module.blog.vo.Blog;
import com.educhains.module.user.service.UserService;
import com.educhains.module.user.service.UserWalletService;
import com.educhains.module.user.vo.User;
import com.educhains.module.user.vo.UserWallet;
import com.educhains.web.util.SessionUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class RewardCtrl {

    private static final Logger logger = LogManager.getLogger(DemoCtrl.class);

    @Autowired
    private BlogService blogService;
    @Autowired
    private UserWalletService userWalletService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/reward", method = RequestMethod.GET)
    public String reward(HttpServletRequest request) {
//        logger.info("log4j demo");
        User sendUser = SessionUtils.getUser(request);
        List<UserWallet> sendList = this.userWalletService.getWalletByUserId(sendUser.getUserId());

        String blogId = request.getParameter("blogId");
        System.out.println("blogId= " + blogId);
        Blog blog = blogService.findBlog(blogId);
        System.out.println("blog.getBlogPub()= " + blog.getBlogPub());

        List<UserWallet> receiveList = this.userWalletService.getWalletByUserId(blog.getBlogPub());
        User receiveUser = this.userService.selectUserById(blog.getBlogPub());
        System.out.println(receiveList.size());

        request.setAttribute("receiver",receiveUser.getUserName());
        request.setAttribute("sender",sendUser.getUserName());
        request.setAttribute("sendAddress",sendList);
        request.setAttribute("receiveAddress", receiveList);

        request.setAttribute("blogId", blogId);

        return "frontend/wallet/reward";
    }
}
